package com.acme.team3.chat;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.acme.team03.messages.TextMessage;
import com.acme.team03.net.server.ConnectionDispatcher;

public class SendCommandHandler implements CommandHandler {

	private ConnectionDispatcher connectionDispatcher;
	private static Logger log = Logger.getLogger(SendCommandHandler.class
			.getName());

	public SendCommandHandler(ConnectionDispatcher connectionDispatcher) {
		this.connectionDispatcher = connectionDispatcher;
	}

	@Override
	public void handle(Command cmd) {
		log.log(Level.INFO, "Message sent: " + cmd.getArg());
		TextMessage textMessage = new TextMessage(cmd.getArg());
		try {
			textMessage.setProperty("Date", new Date());
			textMessage.setProperty("ID", connectionDispatcher
					.getAllConnections().get(0).getID());
			connectionDispatcher.sendAll(textMessage);
		} catch (IOException e) {
			log.log(Level.SEVERE, e.toString());
		}
	}

}
